﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using MenuGame2DXNA;
/*
 * To cindex_ye this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author WIN7
 */
namespace MenuGame2DXNA
{


    public class BombManager : VisibleGameEntity
    {
        /// <summary>
        /// 
        /// </summary>
        private string[] strTexture;


        /// <summary>
        /// 
        /// </summary>
        private short[,] Obstructions;
        /// <summary>
        /// 
        /// </summary>
        private Tiles bootyManager;
        /// <summary>
        /// 
        /// </summary>
        private List<int> BoomFlag;
        /// <summary>
        /// 
        /// </summary>
        const int NoneBomb = 0;
        private Game game;
        private sMainGame MainGame;
        private int bombCount;

        public int BombCount
        {
            get { return bombCount; }
            set { bombCount = value; }
        }
        
        private ContentManager iLayerManager;
        private int TimeBoom;
        private Vector2 SizeCell;
        /// <summary>
        /// Initializes a new instance of the <see cref="BombManager"/> class.
        /// </summary>
        /// <param name="_content">The _content.</param>
        /// <param name="_obstructions">The _obstructions.</param>
        /// <param name="_bootyManager">The _booty manager.</param>
    public BombManager(ContentManager _content, short[,] _obstructions, Tiles _bootyManager,Vector2 _sizeCell,sMainGame _mainGame,int _timeBoom) 
    {
        TimeBoom = _timeBoom;
        Obstructions = _obstructions;
        SizeCell = _sizeCell;
        bootyManager = _bootyManager;
        BoomFlag = new List<int>();
        ObjectVisible = new List<Bomb>();
        bombCount = 0;
        iLayerManager = _content;
       
        strTexture = new string[2];
        strTexture[0] = "Bomb00";
        strTexture[1] = "Bomb01";
        MainGame = _mainGame; 

    }
    public Bomb this[int index]
    {
        get { return ObjectVisible[index]; }
    }
    public int getRealBoomcount()
    {
        int count = 0;
        for (int i = 0; i < BoomFlag.Count; i++)
        {
            if (BoomFlag[i] != NoneBomb)
            {
                count++;
            }
        }
        return count;
    }
    public bool checkbombExist(int index)
    { 
        if(BoomFlag[index] !=NoneBomb)
            return true;
        return false;
    }
    /// <summary>
    /// Tims the kiem cho trong.
    /// </summary>
    /// <returns></returns>
    private int FindSplot() {
        for (int i = 0; i < BoomFlag.Count; i++) {
            if(BoomFlag[i] == NoneBomb)
                return i;
        }
        return -1;
    }
    /// <summary>
    /// Finds the bomb.
    /// </summary>
    /// <param name="pIndex">Index of the p.</param>
    /// <returns></returns>
    public int FindBomb(Point pIndex)
    {
        for(int i = 0; i < bombCount ; i ++)
        {
            if (ObjectVisible[i].PIndex == pIndex)
            {
                return i;
                
            }
        }
        return -1;
    }
    /// <summary>
    /// Bombs the explosion.
    /// </summary>
    /// <param name="pIndex">Index of the p.</param>
        public void BombExplosion(Point pIndex)
        {
            int index = FindBomb(pIndex);
            if(index >= 0 )
                ObjectVisible[index].FlagExp = true;
        }
        /// <summary>
        /// Kiems the tra boom.
        /// </summary>
        /// <param name="LoaiBoom">The loai boom.</param>
        /// <param name="SoLuong">The so luong.</param>
        /// <returns></returns>
    private bool CheckNumberofBombAllowed(int LoaiBoom, int SoLuong) {
        int count = 0;
        for (int i = 0; i < BoomFlag.Count; i++) {
            if (BoomFlag[i] == LoaiBoom) {
                count++;
            }
        }
        return count < SoLuong;
    }

    /// <summary>
    /// Dats the boom.
    /// </summary>
    /// <param name="pIndex">Index of the p.</param>
    /// <param name="LoaiBoom">The loai boom.</param>
    /// <param name="gameTime">The game time.</param>
    /// <param name="value">The value.</param>
    public void AddBoom(Character character, GameTime gameTime, short value)
    {
        if (character.Visible)
        {


            //Kiem Tra so luong boom
            bool pass = true;

            pass = CheckNumberofBombAllowed(character.ID, character.nBombAllow);

            if (pass && Obstructions[character.pIndex.X, character.pIndex.Y] == 0)
            {
                Obstructions[character.pIndex.X, character.pIndex.Y] = value;
                int index = FindSplot();
                Bomb temp = new Bomb(gameTime, iLayerManager, strTexture, character.pIndex, bootyManager, Obstructions, this, SizeCell, "Explosion.xml", character.BombRadius, TimeBoom, MainGame);
                temp.explosion_event += new Bomb.Explosion_handle(Explosion);
                if (index == -1)
                {
                    temp.Index = bombCount;
                    ObjectVisible.Add(temp);
                    BoomFlag.Add(character.ID);
                    bombCount++;
                }
                else
                {
                    temp.Index = index;
                    BoomFlag[index] = character.ID;
                    ObjectVisible[index] = temp;
                }

            }
        }
    }

    /// <summary>
    /// Explosions the specified sender.
    /// </summary>
    /// <param name="Sender">The sender.</param>
      public void Explosion(object Sender)
      {
          
          BoomFlag[((Bomb)Sender).Index] = NoneBomb;      
      }
      /// <summary>
      /// Draws the specified game time.
      /// </summary>
      /// <param name="gameTime">The game time.</param>
      /// <param name="spriteBatch">The sprite batch.</param>
    public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        for (int i = 0; i < bombCount;i++ )
        {

            ObjectVisible[i].Draw(gameTime, spriteBatch);
        }
            base.Draw(gameTime, spriteBatch);
    }

    /// <summary>
    /// Updates the specified game time.
    /// </summary>
    /// <param name="gameTime">The game time.</param>
    /// <param name="color">The color.</param>
    public override void Update(GameTime gameTime, Color color)
    {
        for (int i = 0; i < bombCount; i++)
        {
            ObjectVisible[i].TopLeft = TopLeft;
            ObjectVisible[i].Update(gameTime, color);
        }
       
    }
    
    }
}